<?php

namespace App\Admin\Controllers;

use App\Models\GoldPrize;
use App\Models\User;
use App\Models\Product;
use App\Models\ProductSku;
use App\Models\Address;
use App\Services\MakeProductOrder\MakeProductOrder;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\AdminController;

class GoldPrizesController extends AdminController
{
    protected $title = '发放奖品';

    protected function grid()
    {
        $grid = new Grid(new GoldPrize());
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('user_id', '会员')->display(function () {
            return $this->user ? $this->user->nameText(true) : '';
        });
        $grid->column('product_id', '发放商品')->display(function () {
            return $this->product->title;
        });
        
        
        $grid->disableExport();
        $grid->disableRowSelector();
        
        $this->gridActions($grid);

        return $grid;
    }
    
    protected function gridActions(Grid $grid)
    {
	    $grid->actions(function (Grid\Displayers\Actions $actions) {
	    	$actions->disableView();
	    });
    }
    
    protected function form()
    {
        $form = new Form(new GoldPrize());

        $form->select('user_id', '会员')->options(User::all()->pluck('id', 'id'))
            ->rules('required');

  

         $form->select('product_id', '商品来源')->options(Product::all()->pluck('title', 'id'))
            ->rules('required');

        $this->formTools($form);
        $this->formFooter($form);
        $this->formSaving($form);

        return $form;
    }
    
    protected function formTools(Form $form)
    {
    	$form->tools(function (Form\Tools $tools) {
		    $tools->disableView();
	    });
    }
    
    protected function formFooter(Form $form)
    {
	    $form->footer(function (Form\Footer $footer) {
		    $footer->disableViewCheck();
		    $footer->disableCreatingCheck();
		    $footer->disableEditingCheck();
	    });
    }


    protected function items($skuId)
    {
        return [[
            'sku_id' => $skuId,
            'amount' => 1
        ]];
    }

     protected function formSaving(Form $form)
    {
        $form->saving(function (Form $form) {

            $prize  = GoldPrize::query()->where("user_id",$form->user_id)->first();

            if($prize){
                admin_toastr('该用户已经领取过奖励，不可重复发放', 'error');
                return redirect('/admin/gold-prize');
            }

            $address  = Address::query()->where("user_id",$form->user_id)->where("is_default",true)->first();
            
            if(!$address){
                admin_toastr('当前用户尚未添加地址，不可发放奖励', 'error');
                return redirect('/admin/gold-prize');
            }
            $user = User::find($form->user_id);

           /* if($user->goldRecords()->first()){
                admin_toastr('已参加抢购用户不发放奖励', 'error');
                return redirect('/admin/gold-prize');
            }*/


          
            $sku = ProductSku::query()->where("product_id",$form->product_id)->first();
            $order = app(MakeProductOrder::class, [
                'user' => $user,
                'address' => $address,
                'items' => $this->items($sku->id),
                'record'=>null,
                'isPrize'=>1
            ])->make();
        });
    }
}
